﻿<DemoPageSectionComponent Id="DataGrid-Editing-EditModes" ShowHorizontalScrollBar="true">
    <DemoChildContent>
        @implements IDisposable
        @inject NwindDataService NwindDataService

        @if(Data == null) {
            <p><em>Loading...</em></p>
        } else {
            <DxDataGrid @ref="@grid"
                        Data="@Data"
                        RowRemovingAsync="@OnRowRemovingAsync"
                        RowUpdatingAsync="@OnRowUpdatingAsync"
                        RowInsertingAsync="@OnRowInsertingAsync"
                        InitNewRow="@OnInitNewRowAsync"
                        CssClass="w-100"
                        PageSize="12"
                        KeyFieldName="@nameof(Employee.EmployeeId)"
                        EditMode="@CurrentEditMode">
                <DxDataGridCommandColumn Width="120px" />
                <DxDataGridColumn Field="@nameof(Employee.FirstName)" />
                <DxDataGridColumn Field="@nameof(Employee.LastName)" />
                <DxDataGridColumn Field="@nameof(Employee.Title)" />
                <DxDataGridColumn Field="@nameof(Employee.TitleOfCourtesy)" Width="150px" />
                <DxDataGridDateEditColumn Field="@nameof(Employee.BirthDate)" Width="150px" />
                <DxDataGridDateEditColumn Field="@nameof(Employee.HireDate)" Width="150px" />
            </DxDataGrid>
        }
    </DemoChildContent>
    <OptionsContent>
        <OptionComboBox Label="Edit Mode:" Data="Enum.GetValues<DataGridEditMode>()" @bind-Value="CurrentEditMode" />
    </OptionsContent>

    @code {
        readonly TaskCompletionSource<EditableEmployee> firstEmployeeTcs = new (TaskCreationOptions.RunContinuationsAsynchronously);
        IEnumerable<EditableEmployee> Data { get; set; }
        DxDataGrid<EditableEmployee> grid;

        DataGridEditMode currentEditMode = DataGridEditMode.EditForm;
        DataGridEditMode CurrentEditMode {
            get => currentEditMode;
            set {
                if(currentEditMode != value) {
                    currentEditMode = value;
                    _ = grid?.CancelRowEdit();
                }
            }
        }

        protected override async Task OnInitializedAsync() {
            Data = await NwindDataService.GetEmployeesEditableAsync();
            firstEmployeeTcs.TrySetResult(Data.FirstOrDefault());
        }
        protected override async Task OnAfterRenderAsync(bool firstRender) {
            if(firstRender) {
                var editableEmployee = await firstEmployeeTcs.Task;
                await grid.StartRowEdit(editableEmployee);
            }
        }
        async Task OnRowRemovingAsync(EditableEmployee dataItem) {
            await NwindDataService.RemoveEmployeeAsync(dataItem);
            await UpdateDataAsync();
        }
        async Task OnRowUpdatingAsync(EditableEmployee dataItem, IDictionary<string, object> newValues) {
            await NwindDataService.UpdateEmployeeAsync(dataItem, newValues);
            await UpdateDataAsync();
        }
        async Task OnRowInsertingAsync(IDictionary<string, object> newValues) {
            await NwindDataService.InsertEmployeeAsync(newValues);
            await UpdateDataAsync();
        }
        Task OnInitNewRowAsync(Dictionary<string, object> values) {
            values.Add(nameof(Employee.FirstName), "John");
            values.Add(nameof(Employee.LastName), "Doe");
            return Task.CompletedTask;
        }
        async Task UpdateDataAsync() {
            Data = await NwindDataService.GetEmployeesEditableAsync();
            StateHasChanged();
        }
        public void Dispose() {
            firstEmployeeTcs.TrySetCanceled();
        }
    }
</DemoPageSectionComponent>
